<template>
  <view>
    <!-- 使用搜索组件 -->
    <my-search @click="gotoSearch" :bgcolor="'#c00000'" :radius="15"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧滑动区 -->
      <scroll-view class="scroll-left" :style="{height: wh + 'px'}"  scroll-y="true">
        <!-- <block/> 并不是一个组件，它仅仅是一个包装元素，不会在页面中做任何渲染，只接受控制属性。 -->
        <block v-for="(item, i ) in cateList" :key="i">
          <view @click="changeActive(i)" :class="['left-item', i === active ? 'active' : '']">{{item.cat_name}}</view>
        </block>
        
      </scroll-view>
      <!-- 右侧滑动区 -->
      <scroll-view :scroll-top="scrollTop" class="scroll-right" scroll-y="true" :style="{height: wh + 'px'}">
        <view class="right-item" v-for="(item2, i2) in cateLevel2" :key="i2">
          <!-- 二级分类 标题-->
          <view class="cate-item">/{{item2.cat_name}}/</view>
          <!-- 三级分类 -->
          <view class="lv3-list">
            <view @click="gotoGoodsList(item3)" class="lv3-item" v-for="(item3, i3) in item2.children" :key="i3">
              <!-- 图片 -->
              <image :src="item3.cat_icon" mode=""></image>
              <!-- 文本 -->
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
   //徽标
   mixins:[badgeMix],
    data() {
      return {
        //可用屏幕高度
        wh: 0,
        //分类列表
        cateList: [],
        active: 0,
        //二级分类列表
        cateLevel2: [],
        //滚动条位置
        scrollTop: 0
      };
    },
    
    methods: {
      async getCateList () {
        const {data: res} = await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateLevel2 = res.message[0].children
      },
      changeActive(i) {
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0? 1: 0
      },
      // 跳转到商品详情页
      gotoGoodsList (item) {
        //不能跳转到tabbar页
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      //跳转到搜索页
      gotoSearch () {
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    },
   
    //页面一加载
    onLoad () {
      //获取手机屏幕可用高度
      const sysInfo = uni.getSystemInfoSync()
      // console.log(sysInfo)
      this.wh = sysInfo.windowHeight - 50
      this.getCateList ()
      
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;
    .scroll-left {
      width: 120px;
    }
    .left-item {
      background-color: #f7f7f7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      &.active {
        position: relative;
        background-color: #ffffff;
        &::before {
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    };
    .right-item {
      .cate-item {
        text-align: center;
        font-size: 12px;
        font-weight: bold;
        padding: 15px 0;
      }
      .lv3-list {
        display: flex;
        flex-wrap: wrap;
        .lv3-item {
          margin-bottom: 20px;
          width: 33.33%;
          display: flex;
          flex-direction: column;
          align-items: center;
          image {
            width: 60px;
            height: 60px;
            border: 1px solid red;
            margin-bottom: 10px;
          }
          text {
            font-size: 12px;
          }
        }
      }
    }
  }
  

</style>
